1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92package com.springexample.common.controllers;
import com.springexample.common.constraits.DatabasePasswordSecurerBean;
import com.springexample.common.model.Entity.RegistrationUser;
import com.springexample.common.model.Entity.Role;
import com.springexample.common.model.Entity.User;
import javax.validation.Valid;
import com.springexample.common.service.RoleService;
import com.springexample.common.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import java.util.*;
@Controller
public class RegistrationController {
@Autowired
private UserService userService;
@Autowired
private RoleService roleService;
@Autowired
private DatabasePasswordSecurerBean databasePasswordSecurerBean;
@RequestMapping(value = "/registration", method = RequestMethod.GET)
public ModelAndView registration() {
ModelAndView model = new ModelAndView("registration", "user", new RegistrationUser());
model.addObject("programmingLanguageList", initProgramLang());
model.addObject("countryMap", initCountry());
return model;
}
@RequestMapping(value = "/registration", method = RequestMethod.POST)
public String registration(@Valid @ModelAttribute("user") RegistrationUser user, BindingResult result, Model m) {
if (result.hasErrors()) {
//add objects to model
m.addAttribute("programmingLanguageList", initProgramLang());
m.addAttribute("countryMap", initCountry());
return "registration";
}
Role userRole = roleService.getById(2L); //ROLE_USER
Set<Role> roles = new HashSet<Role>();
roles.add(userRole);
user.setUserRoles(roles);
user.setPasswordHash(databasePasswordSecurerBean.secureUser(user, user.getPassword()));
userService.saveUser((User)user);
m.addAttribute("username", user.getUsername());
return "main";
}
private List<String> initProgramLang() {
List<String> programmingLanguagesList = new ArrayList<String>();
programmingLanguagesList.add("Java");
programmingLanguagesList.add("C#");
programmingLanguagesList.add("C++");
programmingLanguagesList.add("Javascript");
programmingLanguagesList.add("PHP");
programmingLanguagesList.add("Python");
programmingLanguagesList.add("Ruby");
return programmingLanguagesList;
}
private Map<String,String> initCountry() {
Map<String,String> country = new LinkedHashMap<String,String>();
country.put("US", "United Stated");
country.put("RU", "Russia");
country.put("UK", "United Kingdom");
country.put("UA", "Ukraine");
return country;
}
}